package com.example.lastwork;


import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SecondFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SecondFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    List<Map<String, String>> data_list =new ArrayList<>();

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private static SecondFragment mf;
    private String TAG = "second";
    private String text ="";
    private View rootView;
    //单例模式
    public static SecondFragment getSecondFragment()
    {
        if(mf == null){
            mf = new SecondFragment();
        }
        return mf;
    }

    public SecondFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SecondFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SecondFragment newInstance(String param1, String param2) {
        SecondFragment fragment = new SecondFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        if(text==null){
            Bundle args=getArguments();
            text=args.getString("text");
        }
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_second, container,false);
        }
        SharedPreferences sp = getActivity().getSharedPreferences("rate", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        String id = sp.getString("id","o");
        String name1 = sp.getString("name","o");
        editor.apply();

        Log.i(TAG,"连接成功");
        GridView gridView = (GridView) rootView.findViewById(R.id.secondmain);
        new Thread(){
            @Override
            public void run() {
                try {
                    UserDao userDao = new UserDao();
                    userDao.getData(data_list);
                    String[] id2 = userDao.selectlover(id);
                    Log.i(TAG,"成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, "失败");
                }
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        String [] from ={"name","title"};
                        int [] to = {R.id.S_T_id,R.id.S_T_title};
                        SimpleAdapter adapter = new SimpleAdapter(getContext(), data_list, R.layout.sencondgridview, from, to);
                        gridView.setAdapter(adapter);
                        Log.i(TAG, "线程运行");
                    }
                });
            }
        }.start();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // 跳转到另一个界面查看商品详细信息
                Intent intent = new Intent(getActivity(),
                        details.class);
                TextView a = rootView.findViewById(R.id.S_T_id);
                Log.i(TAG, "id:" + a.getText());
                String num = data_list.get(position).get("num");
                Log.i(TAG,num);
                intent.putExtra("num",num);
                String id1 =data_list.get(position).get("id");
                intent.putExtra("id",id1);
                startActivity(intent);
            }
        });
        return  rootView;
    }
}